package com.alpha.eceasy.mall.service.impl;

import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.MallImDTO;
import com.alpha.eceasy.mall.api.entity.MallIm;
import com.alpha.eceasy.mall.api.entity.MallImDetail;
import com.alpha.eceasy.mall.api.vo.MallImVO;
import com.alpha.eceasy.mall.mapper.MallImMapper;
import com.alpha.eceasy.mall.service.MallImService;
import com.alpha.eceasy.mall.util.SecurityUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 聊天会话 服务实现类
 * </p>
 *
 * @author huangpengfei
 * @since 2024-05-15
 */
@Service
public class MallImServiceImpl extends BaseServiceImpl<MallImMapper, MallIm> implements MallImService {
    @Resource
    private MallImDetailServiceImpl imDetailService;
    @Resource
    private MallOrderServiceImpl orderService;
    @Resource
    private MallDemandServiceImpl demandService;
    @Resource
    private MallCustomerServiceImpl customerService;

    public MallImVO getVOByGuid(String orderGuid) {
        MallImVO vo = new MallImVO();
        Optional.ofNullable(orderService.getByGuid(orderGuid)).ifPresentOrElse(order -> {
            vo.setOrderCreateTime(order.getCreateTime());
            Optional.ofNullable(customerService.getByGuid(order.getCustomerGuid()))
                    .ifPresent(customer -> vo.setSellerGuid(customer.getSellerGuid()));
        }, () -> Optional.ofNullable(demandService.getByGuid(orderGuid))
                .ifPresent(demand -> {
                    vo.setOrderCreateTime(demand.getCreateTime());
                    Optional.ofNullable(customerService.getByGuid(demand.getCustomerGuid()))
                            .ifPresent(customer -> vo.setSellerGuid(customer.getSellerGuid()));
                }));
        vo.setMallOrderGuid(orderGuid);
        vo.setImDetailList(new ArrayList<>());
        vo.setState(0);
        MallIm im = this.getByOrderGuid(orderGuid);
        if (Objects.nonNull(im)) {
            BeanUtils.copyProperties(im, vo);
            vo.setImDetailList(imDetailService.listByImGuid(vo.getGuid()));
        }
        return vo;
    }

    public boolean save(MallImDTO dto) {
        MallIm im = this.getOne(Wrappers.<MallIm>lambdaQuery()
                .eq(MallIm::getMallOrderGuid, dto.getMallOrderGuid()));
        if (Objects.isNull(im)) {
            im = new MallIm();
            im.setMallOrderGuid(dto.getMallOrderGuid());
            if (!super.save(im)) {
                throw new BizException("保存失败");
            }
        }
        MallImDetail imDetail = new MallImDetail();
        imDetail.setMallImGuid(im.getGuid());
        if (SecurityUtils.getRoles().contains("USER")) {
            imDetail.setSender(SecurityUtils.getUserGuid());
        } else {
            imDetail.setSender(dto.getSender());
        }
        imDetail.setContentList(dto.getContentList());
        return imDetailService.save(imDetail);
    }

    private MallIm getByOrderGuid(String mallOrderGuid) {
        return this.getOne(Wrappers.<MallIm>lambdaQuery()
                .eq(MallIm::getMallOrderGuid, mallOrderGuid));
    }
}
